package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import com.android.kotlincoroutinemvvmdemo.R

class HeartView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : SurfaceViewTemplate(context, attributeSet) {

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            //style = Paint.Style.STROKE
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_FF0000)
        }
    }

    private val mPath by lazy {
        Path()
    }

    init {
        mHolder.addCallback(this)
    }

    override fun Canvas.onDrawContent() {
        val cx = mWidth.toFloat()
        val cy = mHeight.toFloat()
        //translate(cx, cy)

        drawColor(context.resources.getColor(R.color.color_F3F3F3))

        val mStrokeWidth = (cx * 1 / 20).coerceAtLeast(minStrokeWidth)
        mPaint.strokeWidth = mStrokeWidth

        mPath.apply {
            moveTo(cx / 2, cy / 4)
            cubicTo(cx / 7, cy / 9, cx / 13, cy * 2 / 5, cx / 2, cy * 7 / 12)

            moveTo(cx / 2, cy / 4)
            cubicTo(cx * 6 / 7, cy / 9, cx * 12 / 13, cy * 2 / 5, cx / 2, cy * 7 / 12)

            close()
        }

        drawPath(mPath, mPaint)
    }
}